<?php

namespace App\Console\Commands\OpenShop\Nsq;


use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\OpenShop\Feishu\ShopTagsService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubOpenShopKfosTagsCommand extends Command
{
    protected $signature = 'openshop:nsq-sub-openshop-kfos-tags';

    protected $description = '接受订货的派单状态';

    public function handle()
    {
        ini_set('memory_limit', '4G');
        (new NsqSubscriber(config('nsq.default.address')))
            ->sub('kfos.order.deliveryNote.directDelivered', 'openShop.kfos.delivery.note.tags',
                function (Message $message) {
                    $data = $message->data();
                    Log::info('NSQ当前内存占用：' . (memory_get_usage(true) / 1024 / 1024) . "MB\t" . json_encode($data, JSON_UNESCAPED_UNICODE));
                    // 非 6 的直接退出
                    if ($data['new_order']['sys_type'] != 6 || !($data['shop_id'] ?? 0)) {
                        $message->done();
                        return;
                    }
                    Log::info(
                        'debug_activity_order_delivery_suc', [
                            'data' => $data,
                        ]
                    );
                    $mainProcessModel = OpenShopMainProcessModel::query()
                        ->where('shopId', $data['shop_id'])
                        ->first();
                    if ($mainProcessModel) {
                        (new ShopTagsService($mainProcessModel->orderNo))->updateKfosTag();
                    }
                    $message->done();
                });

    }
}
